<?php
namespace Admin\Controller;
use Think\Controller;
class QuestionController extends CommonController {
	public function indexAction(){
		//查询数据库数据条数
        $sql = "select count(id) count from question";

        $count = current(M()->query($sql))['count'];

        // 分页类
        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show  = $Page->show();// 分页显示输出

        $sql = "select t1.*,t2.username,t2.email from question t1 left join user t2 on t1.uid=t2.id order by t1.uid limit {$Page->firstRow},{$Page->listRows}";

        $res = M()->query($sql);

        var_dump($res);

        foreach ($res as &$v) {

            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['content'] = htmlspecialchars_decode($v['content']);

        }

        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->assign('res',$res);
        $this->display();
	}

	//查看内容
	public function contentAction() {
		$id = I('get.id');
		// $sql = "select title,content from question where id='{$id}'";
		$sql = "select t1.title,t1.content,t2.username,t2.email from question t1 left join user t2 on t1.uid=t2.id where t1.id='{$id}'";

        $res = current(M()->query($sql));
        var_dump($res);
        $res['content'] = htmlspecialchars_decode($res['content']);

        $this->assign('res',$res);
        $this->display();
	}


	//后台不用写添加问题
	// public function addAction(){
 //        if (IS_POST) {
 //            var_dump($_POST);
 //            $id = I('post.id','');
 //            $status = I('post.status','');
 //            $view = I('post.view','');
 //            $answer_num = I('post.answer_num','');

 //            $title = I('post.title','');
 //            $content = I('post.content','');
 //            $create_time = time();            
 //            $uid = I('post.uid','');            
 //            $username = I('post.username','');
 //            $email = I('post.email','');

 //            $usersql = "select email from user where email='{$email}'";
 //            $userres = M()->query($usersql);
 //            if (empty($userres)) {
 //            	$this->error('添加失败,该用户不存在',U('admin/question/index'));
 //            }
 //            $idsql = "select id from user where email='{$email}'";
 //            $idres = M()->query($idsql);
 //            foreach ($idres as &$v) {
            	
 //            }
 //            // var_dump($v['id']);exit;
 //            $sql = "insert into question (title,content,create_time,uid) value('{$title}','{$content}','{$create_time}','{$v['id']}')";
 //            // var_dump($sql);exit;
 //            // var_dump(M()->execute($sql));exit;
 //            if (M()->execute($sql)) {
 //                $this->success('添加成功',U('admin/question/index'));
 //            }else{
 //                $this->error('添加失败',U('admin/question/index'));
 //            }
 //        }
 //    	$this->display();
 //    }

	public function editAction() {
		if (IS_POST) {
       		var_dump($_POST);
       		$title = I('post.title');
       		$content = I('post.content');
       		$id = I('post.id','');
       		$sql = "update question set title='{$title}',content='{$content}' where id='{$id}'";
            $res = M()->execute($sql);
            if ($res) {
            	$this->success('修改成功',U('admin/question/index'));
            }else{
            	$this->error('修改失败',U('admin/question/index'));
            }
        }else{
        	$id = I('get.id','');
			if (empty($id)) {
				$this->error('非法请求',U('admin/question/index'));
			}else{
				$sql = "select title,content,id from question where id='{$id}'";
				$res = M()->query($sql);
	            $res = current(M()->query($sql));
	        }
        }	       
        $this->assign('res',$res);
        $this->assign('cityres',$cityres);
        $this->display();
	}

	public function delAction(){
    	$id = I('get.id');

        $sql = "delete from question where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/question/index'));
        }else{
            $this->error('删除失败',U('admin/question/index'));
        }
    }

	//停用状态
    // public function stopAction() {
    //     $id = I('get.id');        
    //     $sql = "update question set status='0' where id='{$id}'";
    //     if (M()->execute($sql)) {
    //         $this->success('停用成功',U('admin/question/index'));
    //     }else{
    //         $this->error('停用失败',U('admin/question/index'));
    //     }       
    // }

    // //启用状态
    // public function upAction() {
    //     $id = I('get.id');        
    //     $sql = "update question set status='1' where id='{$id}'";
    //     if (M()->execute($sql)) {
    //         $this->success('启用成功',U('admin/question/index'));
    //     }else{
    //         $this->error('启用失败',U('admin/question/index'));
    //     }      
    // }


}